using System;
using System.Data;
//Using PCS's Namespaces 

using PCSComUtils.Common;
using PCSComUtils.PCSExc;
using PCSComProduction.ValueAddTransact.DS;

namespace PCSComProduction.ValueAddTransact.BO
{
	public class OutsideProcessingBO //:  IOutsideProcessingBO 
	{
		#region Declaration
		
		private const string THIS = "PCSComSale.Order.BO.SaleOrderBO";

		#endregion Declaration

		#region Constructor, Destructor

		public OutsideProcessingBO()
		{			
		}

		#endregion Constructor, Destructor

		#region Update Data Methods
		
		#region IObjectBO's Members
		//[AutoComplete(true)]
		public void Add(object pObjectDetail){}
		
		/// <summary>
		/// Delete record by condition
		/// </summary>
		//[AutoComplete(true)]
		public void Delete(object pObjectVO){}
		/// <summary>
		/// Get the object information by ID of VO class
		/// </summary>
		
		//[AutoComplete(true)]
		public object GetObjectVO(int pintID, string VOclass)
		{
			return null;
		}
		
		/// <summary>
		/// Update into Database
		/// </summary>
		//[AutoComplete(true)]
		public void Update(object pObjectDetail){}

		//[AutoComplete(true)]
		public void UpdateDataSet(DataSet p_dtsMaster){}

		#endregion IObjectBO's Members

		/// <summary>
		/// Add new outside processing
		/// </summary>
		/// <param name="pobjMaster"></param>
		/// <param name="pdtbDetail"></param>
				
		//[AutoComplete(true)]
		public void AddOutsideProcessing(object pobjMaster, DataTable pdtbDetail)
		{
			try
			{
				PRO_OutsideProcessingMasterDS dsMaster = new PRO_OutsideProcessingMasterDS();
				int intOPMasterId = dsMaster.AddAndReturnID(pobjMaster);
				
				//reset master id of voObject
				((PRO_OutsideProcessingMasterVO)pobjMaster).OutsideProcessingMasterID = intOPMasterId;

				if(pdtbDetail != null)
				{
					foreach (DataRow row in pdtbDetail.Rows)
					{
						if(row.RowState == DataRowState.Deleted) continue;
						row[PRO_OutsideProcessingDetailTable.OUTSIDEPROCESSINGMASTERID_FLD] = intOPMasterId;
					}
					PRO_OutsideProcessingDetailDS dsDetail = new PRO_OutsideProcessingDetailDS();
					
					//Check dataset to call UpdateDataSet method
					if(pdtbDetail.DataSet != null)
					{
						dsDetail.UpdateDataSet(pdtbDetail.DataSet);
					}
					else
					{
						DataSet dtsDetail = new DataSet();
						dtsDetail.Tables.Add(pdtbDetail);
						dsDetail.UpdateDataSet(dtsDetail);
					}
				}
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		/// <summary>
		/// Update outside processing
		/// </summary>
		/// <param name="pobjMaster"></param>
		/// <param name="pdtbDetail"></param>
		//[AutoComplete(true)]
		public void UpdateOutsideProcessing(object pobjMaster, DataTable pdtbDetail)
		{
			try
			{
				PRO_OutsideProcessingMasterDS dsOPMaster = new PRO_OutsideProcessingMasterDS();
				dsOPMaster.Update(pobjMaster);
				
				//check and update master id for new added rows
				if(pdtbDetail != null)
					foreach (DataRow row in pdtbDetail.Rows)
					{
						if(row.RowState == DataRowState.Deleted) continue;
						row[PRO_OutsideProcessingMasterTable.OUTSIDEPROCESSINGMASTERID_FLD] = ((PRO_OutsideProcessingMasterVO)pobjMaster).OutsideProcessingMasterID;
					}

					PRO_OutsideProcessingDetailDS dsDetail = new PRO_OutsideProcessingDetailDS();
					//Check dataset to call UpdateDataSet method
					if(pdtbDetail.DataSet != null)
					{
						dsDetail.UpdateDataSet(pdtbDetail.DataSet);
					}
					else
					{
						DataSet dtsDetail = new DataSet();
						dtsDetail.Tables.Add(pdtbDetail);
						dsDetail.UpdateDataSet(dtsDetail);
					}
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		/// <summary>
		/// Delete outside processing
		/// </summary>
		/// <param name="pintMasterID"></param>
		//[AutoComplete(true)]
		public void DeleteOutsideProcessing(int pintMasterID)
		{			
			try
			{
				//First, delete data on detail table
				PRO_OutsideProcessingDetailDS dsOPDetail = new PRO_OutsideProcessingDetailDS();				
				DataTable dtbDetail = dsOPDetail.GetDetailByMaster(pintMasterID);

				foreach(DataRow drow in dtbDetail.Rows)
				{
					dsOPDetail.Delete(int.Parse(drow[PRO_OutsideProcessingDetailTable.OUTSIDEPRCESSINGDETAILID_FLD].ToString()));
				}

				//Then delete data on master table
				PRO_OutsideProcessingMasterDS dsOPMaster = new PRO_OutsideProcessingMasterDS();
				dsOPMaster.Delete(pintMasterID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		#endregion Update Data Methods

		#region Retrieve Data Methods
		/// <summary>
		/// Call GetMasterOutsideProcessing from OutsideProcessingMasterDS
		/// </summary>
		/// <param name="pintMasterID"></param>
		/// <returns></returns>
		//[AutoComplete(true)]
		public DataRow GetOutsideProcessingMaster(int pintMasterID)
		{
			try
			{
				PRO_OutsideProcessingMasterDS dsOPMaster = new PRO_OutsideProcessingMasterDS();
				return dsOPMaster.GetOutsideProcessingMaster(pintMasterID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}
		/// <summary>
		/// Get list of record from OutsideProcessingDetailID to load to grid on form
		/// </summary>
		/// <param name="pintMasterID"></param>
		/// <returns></returns>
		//[AutoComplete(true)]
		public DataTable GetDetailByMaster(int pintMasterID)
		{
			try
			{
				PRO_OutsideProcessingDetailDS dsOPDetail = new PRO_OutsideProcessingDetailDS();
				return dsOPDetail.GetDetailByMaster(pintMasterID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		#endregion Retreive Data Methods
	}
}
